﻿#region License

// Another free, open solution by someone who gets it.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

using Patterns.SafeCode;

namespace Patterns.Reflection
{
	/// <summary>
	/// 	Provides instance-based extensions for the <see cref = "Type" /> object.
	/// </summary>
	public static class TypeExtensions
	{
		///<summary>
		///	Determines whether or not the current type is a valid implementation; that is, whether or not
		///	the current type is the same as or derives / implements the target type, and whether or not
		///	it is creatable &amp; therefore usable.
		///</summary>
		///<param name = "type">The current type.</param>
		///<typeparam name = "T">The target type.</typeparam>
		///<returns>True if the current type is a valid implementation of the target type; otherwise, false.</returns>
		public static bool IsValidImplementationOf<T>(this Type type)
		{
			return type.IsValidImplementationOf(typeof (T));
		}

		///<summary>
		///	Determines whether or not the current type is a valid implementation; that is, whether or not
		///	the current type is the same as or derives / implements the target type, and whether or not
		///	it is creatable &amp; therefore usable.
		///</summary>
		///<param name = "type">The current type.</param>
		///<param name = "targetType">The target type.</param>
		///<returns>True if the current type is a valid implementation of the target type; otherwise, false.</returns>
		public static bool IsValidImplementationOf(this Type type, Type targetType)
		{
			if (!targetType.IsAssignableFrom(type)) return false;
			return !(type.IsAbstract || type.IsInterface);
		}

		/// <summary>
		/// 	Determines whether the specified type is decorated with the indicated attribute.
		/// </summary>
		/// <typeparam name = "T"></typeparam>
		/// <param name = "type">The type.</param>
		/// <returns>
		/// 	<c>true</c> if the specified type is decorated; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsDecorated<T>(this Type type) where T : Attribute
		{
			return type.GetAttributes<T>().Any();
		}

		/// <summary>
		/// 	Gets the attributes decorating this type.
		/// </summary>
		/// <typeparam name = "T"></typeparam>
		/// <param name = "type">The type.</param>
		/// <returns></returns>
		public static IEnumerable<T> GetAttributes<T>(this Type type) where T : Attribute
		{
			IEnumerable<T> results = Try.Get(() => type.GetCustomAttributes(typeof (T), true).OfType<T>());
			return results ?? new T[] { };
		}

		/// <summary>
		/// 	Gets the display name.
		/// </summary>
		/// <param name = "type">The type.</param>
		/// <returns></returns>
		public static string GetDisplayName(this Type type)
		{
			if (type.IsGenericType)
			{
				string nonGenericName = Try.Get(() => Regex.Match(type.Name, "\\w+").Value);
				string genericArgList = string.Join(", ", type.GetGenericArguments().Select(t => t.GetDisplayName()).ToArray());
				return string.Format("{0}<{1}>", nonGenericName, genericArgList);
			}

			return type.Name;
		}
	}
}